<template>
  <div class="container">
    <el-card class="box-card">
      <section>
        <el-row>
          <el-form
            ref="elForm2"
            :model="formData"
            :rules="rules"
            :label-position="labelPosition"
            size="medium"
            label-width="100px"
            class="form-style"
          >
            <el-row type="flex" style="margin: 20px 0">
              <el-form-item label="产品" prop="productsName">
                <el-select v-model="formData.status" placeholder="请选择产品">
                  <el-option
                    v-for="item in statusList"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                    clearable
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="时间">
                <el-date-picker
                  type="date"
                  placeholder="选择日期"
                  v-model="formData.date1"
                  style="width: 100%"
                ></el-date-picker>
              </el-form-item>
            </el-row>
            <el-row type="flex" align="top">
              <el-form-item label="数量" prop="productsNums">
                <el-input
                  v-model="formData.productsNums"
                  placeholder="请输入任务数量"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
              <el-form-item label="单位" prop="productsNums">
                <el-input
                  v-model="formData.productsNums"
                  placeholder="请输入单位"
                  clearable
                  :style="{ width: '100%' }"
                >
                </el-input>
              </el-form-item>
            </el-row>
            <el-row align="top" type="flex">
              <el-form-item label="工单编号" prop="productsName">
                <el-input
                  v-model="formData.productsName"
                  placeholder="请输入工单编号"
                  clearable
                >
                </el-input>
              </el-form-item>
              <el-form-item label="结算方式" prop="productsName">
                <el-select
                  v-model="formData.settle"
                  placeholder="请选择结算方式"
                >
                  <el-option
                    v-for="item in settleType"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                    clearable
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="备注 " prop="remark">
                <el-input
                  v-model="formData.remark"
                  type="textarea"
                  placeholder="请输入备注 "
                  :autosize="{ minRows: 4, maxRows: 4 }"
                  :style="{ width: '100%' }"
                ></el-input>
              </el-form-item>
            </el-row>
            <el-row>
              <el-table :data="tableData" border style="width: 60%">
                <el-table-column type="index" label="序号"> </el-table-column>
                <el-table-column prop="name" label="工序"> </el-table-column>
                <el-table-column prop="name" label="计价单价">
                </el-table-column>
                <el-table-column prop="name" label="计时单价">
                </el-table-column>
                <el-table-column prop="address" label="参与员工">
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="100">
                  <template slot-scope="scope">
                    <el-button
                      @click="dialogTableVisibleProcess = true"
                      type="text"
                      size="small"
                      >编辑</el-button
                    >
                    <el-button
                      type="text"
                      size="small"
                      @click="
                        handleClickDel(scope.$index, scope.row.furnitureId)
                      "
                      >删除</el-button
                    >
                  </template>
                </el-table-column>
              </el-table>
            </el-row>
            <el-row style="margin: 3px 0">
              <el-button
                size="mini"
                type="success"
                @click="dialogTableVisibleProcess = true"
                >新增</el-button
              >
            </el-row>
          </el-form>
        </el-row>
        <el-dialog
          :visible.sync="dialogTableVisibleProcess"
          v-bind="$attrs"
          v-on="$listeners"
          width="30%"
          title="修改"
          @close="dialogTableVisibleProcess = false"
        >
          <el-form
            ref="elForm2"
            :model="formDataProcess"
            size="medium"
            label-width="100px"
          >
            <el-form-item label="工序" prop="productsName">
              <el-select
                v-model="formData.status"
                style="width: 180px"
                placeholder="请选择工序"
              >
                <el-option
                  v-for="item in statusList"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                  clearable
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="单价" prop="productsNums">
              <el-input
                v-model="formData.productsNums"
                placeholder="请输入单价"
                clearable
                style="width: 180px"
              >
              </el-input>
            </el-form-item>
            <el-form-item label="参与员工" prop="productsName">
              <el-select
                style="width: 180px"
                v-model="formData.status"
                placeholder="请选择员工"
              >
                <el-option
                  v-for="item in statusList"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                  clearable
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer">
            <el-button @click="dialogTableVisibleProcess = false"
              >取消</el-button
            >
            <el-button type="primary" @click="dialogTableVisibleProcess = false"
              >确定</el-button
            >
          </div>
        </el-dialog>
      </section>
      <footer>
        <div style="text-align: center">
          <el-button @click="onClose">取消</el-button>
          <el-button type="primary" @click="handleConfirm"
            >保存并发布</el-button
          >
        </div>
      </footer>
    </el-card>
  </div>
</template>

<script>
import {
  searchTasks,
  removeTask,
  getCraftInfo,
  searchUsers,
  addTask,
  editTask,
} from "@/api/index.js";

export default {
  components: {},
  data() {
    return {
      labelPosition: "right",
      tableData: [],
      title: "工单发布",
      formDataProcess: {},
      dialogTableVisibleProcess: false,
      dialogTableVisible: false,
      formData: {
        productsName: undefined,
        productsNums: undefined,
        // status: undefined,
        remark: undefined,
        craftId: undefined,
        // employees: [],
        craftInfos: [],
      },
      rules: {
        furnitureId: [
          {
            required: true,
            message: "请选择工序",
            trigger: "blur",
          },
        ],
        employees: [
          {
            required: true,
            message: "请选择员工",
            trigger: "blur",
          },
        ],
        productsName: [
          {
            required: true,
            message: "请输入工单编号",
            trigger: "blur",
          },
        ],
        productsNums: [
          {
            required: true,
            message: "请输入任务数量",
            trigger: "blur",
          },
        ],
        status: [
          {
            required: true,
            message: "请输入工单状态",
            trigger: "blur",
          },
        ],
        remark: [
          {
            required: true,
            message: "请输入备注 ",
            trigger: "blur",
          },
        ],
      },
      furnitureId: [],
      employeeId: [],
      statusList: [
        { id: 0, value: "0", label: "生产中" },
        { id: 1, value: "1", label: "生产完成" },
      ],
      settleType: [
        { id: 0, value: "0", label: "计时" },
        { id: 1, value: "1", label: "计件" },
      ],

      showReleaseTask: false,
      dataTime: "",
      releaseTaskType: "",
      searchData: {
        taskStartTime: undefined,
        taskEndTime: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
      editForm: {},
      multipleSelection: [],
      value: [],
      optionsProps: {
        multiple: true,
      },
      options: [],
      showCascader: false,
      mouseValue: "",
    };
  },
  created() {
    this.getTaskList();
  },
  methods: {
    async getCraftInfoApi() {
      const { data: res } = await getCraftInfo();
      this.furnitureId = res;
      let arr = [];
      res.forEach((item) => {
        arr.push({ label: item.craftName, value: item.craftId });
      });
      this.options = arr;
    },
    async getUserList() {
      const { data: res } = await searchUsers();
      this.employeeId = res;
      let newChild = res;
      let arr = [];
      newChild.forEach((item) => {
        arr.push({ label: item.employeeName, value: item.employeeId });
      });
      this.options.forEach((res) => {
        res.children = arr;
      });
      this.$nextTick(() => {
        this.$set(this.options[0].children, arr);
      });
      this.showCascader = true;
    },
    changeDataTime(e) {
      if (e != null) {
        this.searchData.taskStartTime = e[0];
        this.searchData.taskEndTime = e[1];
      } else {
        this.searchData.taskStartTime = undefined;
        this.searchData.taskEndTime = undefined;
      }
    },
    clearForm() {
      this.searchData = {};
      this.getTaskList(1);
    },
    postTask() {
      this.showReleaseTask = true;
      this.releaseTaskType = "新增";
      this.$refs.releaseTask.onOpen();
    },
    // 添加
    async handleClick() {
      await this.getCraftInfoApi();
      await this.getUserList();
      this.title = "工单发布";
      this.dialogTableVisible = true;
    },
    async handleClickEdit(row) {
      await this.getCraftInfoApi();
      await this.getUserList();
      this.title = "修改生产任务";
      this.formData = row;
      // let employees = row.employees;
      // employees = employees.map((item) => item.employeeId);
      // this.formData.employees = row.crafts;
      console.log(row.map.CraftInfo);
      let craftInfo = row.map.CraftInfo;
      let newArr = [];
      let arr = [];
      craftInfo.forEach((res) => {
        newArr.push({
          craftId: res.craftId,
          employeeId: res.employeeId,
        });
      });
      console.log(newArr);
      newArr.forEach((item) => {
        arr.push(Object.values(item)); //对象转化为数组
      });
      console.log(arr);
      this.value = arr;
      this.dialogTableVisible = true;
    },
    async handleClickDel(index, e) {
      const res = await removeTask({ furnitureId: e });
      console.log(res);
      this.tableData.splice(index, 1);
    },
    handleConfirm() {
      this.$refs["elForm2"].validate((valid) => {
        if (!valid) return;
        this.formData.craftInfos = this.value;
        console.log("请求参数", this.formData);
        if (this.title == "修改生产任务") {
          this.updateTaskFun();
        } else {
          this.addTaskFun();
        }
      });
    },
    async addTaskFun() {
      const res = await addTask(this.formData);
      console.log(res);
      if (res.code === 200) {
        this.$message.success("发布成功");
        this.onClose();
      }
    },
    // 修改
    async updateTaskFun() {
      let params = {
        craftInfos: this.formData.craftInfos,
        productsName: this.formData.productsName,
        productsNums: this.formData.productsNums,
        remark: this.formData.remark,
        status: this.formData.status,
        furnitureId: this.formData.furnitureId,
        // employees: this.formData.employees,
        taskCreateTime: this.formData.taskCreateTime,
      };
      const res = await editTask(params);
      console.log(res);
      if (res.code === 200) {
        this.$message.success("修改任务成功");
        this.onClose();
      }
    },

    close() {
      this.dialogTableVisible = false;
      this.getTaskList();
    },
    onClose() {
      this.dialogTableVisible = false;
      this.formData = {
        productsName: undefined,
        productsNums: undefined,
        status: undefined,
        remark: undefined,
        craftId: undefined,
        craftInfos: [],
      };
      this.value = [];
      this.showCascader = false;
      console.log(this.formData);
      this.getTaskList(1);
    },
    // 多选
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => item.furnitureId);
    },
    //批量删除
    async deleteAll() {
      console.log(this.multipleSelection);
      let list = this.multipleSelection;
      list = list.join(",");
      this.multipleSelection = list;
      const res = await removeTask({ furnitureId: this.multipleSelection });
      this.getTaskList(1);
    },
    async getTaskList(page) {
      this.searchData.pageNum = page == null ? this.searchData.pageNum : page;
      console.log(this.searchData);
      const res = await getCraftInfo(this.searchData);
      console.log("工序列表", res.data);

      this.tableData = res.data;
      this.total = res.total;
    },
    //分页修改每页显示条数
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getTaskList();
    },
    //分页跳转页面
    handleCurrentChange(val) {
      this.searchData.pageNum = val;
      this.getTaskList();
    },
    handleChange(value) {
      console.log(value);
    },
    mouseenterplus(item) {
      console.log(item);
      this.mouseValue = item;
    },
  },
};
</script>

<style>
.row-bg {
  padding: 10px 0;
}
.box-card {
  height: 100%;
}
.popover-content {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: 3px;
}
.el-button--mini.is-circle {
  padding: 2px !important;
}
</style>
